<template>
  <div v-if="isOpen" class="modal modal-open">
    <div class="modal-box max-w-2xl">
      <h3 class="font-bold text-lg mb-4">
        {{ trip?.id ? '编辑行程' : '新建行程' }}
      </h3>
      
      <form @submit.prevent="handleSubmit" class="space-y-4">
        <div class="form-control">
          <label class="label">
            <span class="label-text">行程标题</span>
          </label>
          <input
            v-model="formData.title"
            type="text"
            placeholder="请输入行程标题"
            class="input input-bordered w-full"
            required
          />
        </div>

        <div class="form-control">
          <label class="label">
            <span class="label-text">行程描述</span>
          </label>
          <textarea
            v-model="formData.description"
            placeholder="请输入行程描述"
            class="textarea textarea-bordered h-24"
            required
          ></textarea>
        </div>

        <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
          <div class="form-control">
            <label class="label">
              <span class="label-text">开始日期</span>
            </label>
            <input
              v-model="formData.startDate"
              type="date"
              class="input input-bordered"
              required
            />
          </div>

          <div class="form-control">
            <label class="label">
              <span class="label-text">结束日期</span>
            </label>
            <input
              v-model="formData.endDate"
              type="date"
              class="input input-bordered"
              required
            />
          </div>

          <div class="form-control">
            <label class="label">
              <span class="label-text">行程时长（天）</span>
            </label>
            <select v-model="formData.duration" class="select select-bordered" required>
              <option v-for="duration in TRIP_DURATION_OPTIONS" :key="duration" :value="duration">
                {{ duration }}天
              </option>
            </select>
          </div>

          <div class="form-control">
            <label class="label">
              <span class="label-text">行程状态</span>
            </label>
            <select v-model="formData.status" class="select select-bordered" required>
              <option v-for="status in STATUS_OPTIONS" :key="status" :value="status">
                {{ status }}
              </option>
            </select>
          </div>
        </div>

        <div class="form-control">
          <label class="label">
            <span class="label-text">景点选择</span>
          </label>
          <div class="flex gap-2">
            <button
              type="button"
              @click="openSpotSelector"
              class="btn btn-outline"
            >
              选择景点
            </button>
            <div v-if="formData.spots.length" class="flex-1 flex flex-wrap gap-2">
              <div
                v-for="spot in formData.spots"
                :key="spot"
                class="badge badge-primary"
              >
                {{ spot }}
              </div>
            </div>
          </div>
        </div>

        <div class="modal-action">
          <button type="button" class="btn" @click="closeModal">取消</button>
          <button type="submit" class="btn btn-primary">保存</button>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import SpotSelector from './SpotSelector.vue'

const STATUS_OPTIONS = ['计划中', '进行中', '已完成', '已取消'] as const
const TRIP_DURATION_OPTIONS = [1, 2, 3, 4, 5, 6, 7, 14, 30] as const

interface Props {
  isOpen: boolean
  trip: {
    id?: string
    title: string
    description: string
    startDate: string
    endDate: string
    duration: number
    status: typeof STATUS_OPTIONS[number]
    spots: string[]
  } | null
}

const props = defineProps<Props>()
const emit = defineEmits(['update:isOpen', 'save'])

const formData = ref({
  title: '',
  description: '',
  startDate: '',
  endDate: '',
  duration: 1,
  status: '计划中' as typeof STATUS_OPTIONS[number],
  spots: [] as string[]
})

watch(() => props.trip, (newTrip) => {
  if (newTrip) {
    formData.value = { ...newTrip }
  }
}, { immediate: true })

function closeModal() {
  emit('update:isOpen', false)
}

function handleSubmit() {
  emit('save', { ...formData.value })
}

function openSpotSelector() {
  // TODO: 实现景点选择器的打开逻辑
}
</script> 